package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.util.AppJwtUtil;
import com.heima.media.dto.LoginDto;
import com.heima.media.entity.WmUser;
import com.heima.media.mapper.WmUserMapper;
import com.heima.media.service.IWmUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 自媒体用户信息表 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-08-07
 */
@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements IWmUserService {

    //保存自媒体用户
    public ResponseResult<WmUser> saveWmUser(WmUser user) {
        //判断参数
        if (user == null || user.getApUserId() == null || StringUtils.isEmpty(user.getName())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        LambdaQueryWrapper<WmUser> qw=new LambdaQueryWrapper<>();
        qw.eq(WmUser::getApUserId,user.getApUserId());
        WmUser one = this.getOne(qw);
        //存在返回
        if (one!=null) {
            return ResponseResult.okResult(one);
        }
        //不存在更新
        user.setCreatedTime(new Date());
        this.save(user);
        return ResponseResult.okResult(user);
    }

    //登录
    public ResponseResult login(LoginDto dto) {
        //判断参数
        if (dto == null || StringUtils.isEmpty(dto.getName()) || StringUtils.isEmpty(dto.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //根据用户名查询
        LambdaQueryWrapper<WmUser> qw=new LambdaQueryWrapper<>();
        qw.eq(WmUser::getName,dto.getName());
        WmUser user = this.getOne(qw);
        //不存在,提示错误信息
        if (user==null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //存在，获取盐，使用用户传入的密码+盐，md5计算
        String salt = user.getSalt();
        String pwdStr=dto.getPassword()+ salt;
        String md5Str= DigestUtils.md5DigestAsHex(pwdStr.getBytes());
        //判断数据
        if (!md5Str.equals(user.getPassword())) {
            //返回错误
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }
        //一致，生成token，使用用户id生成
        Map<String,Object> map=new HashMap<>();
        map.put("userId",user.getId());
        map.put("userName",user.getName());
        String token = AppJwtUtil.getToken(map);
        // 定义返回的结果
        Map<String, Object> map1 = new HashMap<>();
        // 脱敏处理
        user.setPhone(null);
        user.setPassword(null);
        user.setSalt(null);
        map1.put("user", user);
        map1.put("token", token);
        // 通用的响应
        return ResponseResult.okResult(map1);
    }
}
